﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Web.Configuration;
using System.Text;
using System.IO;
using System.Collections.Generic;
//using System.Windows.Forms;

public partial class Admin_ProductActive : System.Web.UI.Page
{
    ShopLaptopConnection conn;
    //Sqlconnion conn;
    protected void Page_Load(object sender, EventArgs e)
    {
        conn = new ShopLaptopConnection();
        //conn = new Sqlconnion(WebConfigurationManager.connionStrings["ShopLaptopconnionString"].connionString);
        if (!IsPostBack)
        {
            if (Session["Admin"] == null)
            {
                Response.Redirect("Login.aspx");
            }
            List<Category> list = conn.GetAllCategoryByStatus1();
            foreach (Category cate in list)
            {
                ListItem item = new ListItem(cate.CateName,cate.CateID.ToString());
                drdlCateViewPro.Items.Add(item);
                drdlCategory.Items.Add(item);
            }
        }
    }

   
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        string proID = GridView1.Rows[GridView1.SelectedIndex].Cells[0].Text;
        string proName = GridView1.Rows[GridView1.SelectedIndex].Cells[1].Text;
        string proDesc = GridView1.Rows[GridView1.SelectedIndex].Cells[2].Text;
        string proPrice = GridView1.Rows[GridView1.SelectedIndex].Cells[4].Text;
        string proImage = "";
        try
        {
            proImage = GridView1.Rows[GridView1.SelectedIndex].Cells[5].Text;
        }
        catch (Exception)
        {
        }
        
        string proWarranty = GridView1.Rows[GridView1.SelectedIndex].Cells[6].Text;
        
        txtProID.Text = proID;
        txtProName.Text = proName;
        txtProDesc.Text = proDesc;
        txtProPrice.Text = proPrice;
        //FileUpload1.FileName = ProImage;
        txtProWar.Text = proWarranty;
        
;
        drdlCategory.SelectedIndex = drdlCateViewPro.SelectedIndex;
    }

    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        int check = 0;
        int proID = Convert.ToInt32(txtProID.Text);
        string proName = txtProName.Text;
        string proDesc = txtProDesc.Text;
        float proPrice = Convert.ToInt32(txtProPrice.Text);
        //string ProImage = txtProImage.Text;
        string proImage = "";
        proImage = FileUpload1.FileName;
        int proWarranty = Convert.ToInt32(txtProWar.Text);
        int cateID = Convert.ToInt32(drdlCategory.SelectedValue);
        check = conn.UpdateProductById(proID,proName,proDesc,proPrice,proImage,proWarranty,cateID);
        if (check > 0)
        {
            ShowPopUpMsg("Update product success!");
        }
        DisplayByCategory();
    }

    private void ShowPopUpMsg(string msg)
    {
        StringBuilder sb = new StringBuilder();
        sb.Append("alert('");
        sb.Append(msg.Replace("\n", "\\n").Replace("\r", "").Replace("'", "\\'"));
        sb.Append("');");
        ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "showalert", sb.ToString(), true);
    }

//    protected void Display()
//    {
//        GridView1.DataSource = "";
//        GridView1.DataBind();
//        //string CateID = drdlCateViewPro.SelectedValue;
//        SqlDataAdapter da = new SqlDataAdapter(@"Select * from Product
//                    where proStatus=1",conn);
//        DataTable dt = new DataTable();
//        da.Fill(dt);
//        if (dt.Rows.Count > 0)
//        {
//            GridView1.DataSource = dt;
//            GridView1.DataBind();
//        }
//    }

    protected void DisplayByCategory()
    {
        GridView1.DataSource = "";
        GridView1.DataBind();
        List<Product> list = conn.GetProduct1ByCateID(Convert.ToInt32(drdlCateViewPro.SelectedValue));
        GridView1.DataKeyNames = new string[] { "ProID" };
        GridView1.DataSource = list;
        GridView1.DataBind();
    } 
    protected void drdlCateViewPro_SelectedIndexChanged(object sender, EventArgs e)
    {
        GridView1.DataSource = "";
        GridView1.DataBind();
        try
        {
            List<Product> list = conn.GetProduct1ByCateID(Convert.ToInt32(drdlCateViewPro.SelectedValue));
            GridView1.DataKeyNames = new string[] { "ProID" };
            GridView1.DataSource = list;
            GridView1.DataBind();
        }
        catch { }
    
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        //GridView1.PageIndex = e.NewPageIndex;
    }
    protected void btnAddProduct_Click(object sender, EventArgs e)
    {
        int check = 0;
        //int ProID = Convert.ToInt32(txtProID.Text);
        string proName = txtProName.Text;
        string proDesc = txtProDesc.Text;
        DateTime proDate = DateTime.Now;
        float proPrice = Convert.ToInt32(txtProPrice.Text);
        //string ProImage = txtProImage.Text;
        string proImage = FileUpload1.FileName;
        int proWarranty = Convert.ToInt32(txtProWar.Text);
        int cateID = Convert.ToInt32(drdlCategory.SelectedValue);
        check = conn.InsertProduct(proName, proDesc, proDate, proPrice, proImage, proWarranty, cateID);
        if (check > 0)
        {
            ShowPopUpMsg("Insert product success!");
        }
        DisplayByCategory();
    }
    
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        int check = 0;
        int id = Convert.ToInt32(GridView1.Rows[e.RowIndex].Cells[0].Text);
        check = conn.SetProductStatus0(id);
        if (check > 0)
        {
            DisplayByCategory();
        }
    }
}
